package com.easyexcel.controller;

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.easyexcel.domain.ExportInfo;
import com.easyexcel.domain.MultiLineHeadExcelModel;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 类描述:
 *
 * @author jianweiwang
 * @version v1.0
 * @link wjw@choicesoft.com.cn
 * @date 2019/3/29 18:21
 **/
@RestController
public class WriteExcelController {

    @RequestMapping(value = "/writeExcelBySingle", method = RequestMethod.GET)
    public void writeExcel1() throws IOException {
        List<ExportInfo> list = getList();
        String sheetName = "第一个 sheet";

        OutputStream out = new FileOutputStream("E:\\myself\\easyexcel-demo\\src\\main\\resources\\2008.xlsx");
        ExcelWriter writer = new ExcelWriter(out,ExcelTypeEnum.XLSX);
        Sheet sheet = new Sheet(1,0,ExportInfo.class);
        sheet.setSheetName(sheetName);
        writer.write(list,sheet);
        writer.finish();
    }
    @RequestMapping(value = "/writeExcelByMuliti", method = RequestMethod.GET)
    public void writeExcel2() throws IOException {
        List<MultiLineHeadExcelModel> list = getMulitiList();
        String sheetName2 = "第二个 sheet";

        OutputStream out = new FileOutputStream("E:\\myself\\easyexcel-demo\\src\\main\\resources\\2008.xlsx");
        ExcelWriter writer = new ExcelWriter(out,ExcelTypeEnum.XLSX);

        Sheet sheet = new Sheet(2,0,MultiLineHeadExcelModel.class);
        sheet.setSheetName(sheetName2);
        writer.write(list,sheet);
        writer.finish();
    }

    private List<MultiLineHeadExcelModel> getMulitiList() {
        List<MultiLineHeadExcelModel> list = new ArrayList<>();
        MultiLineHeadExcelModel model = new MultiLineHeadExcelModel();
        model.setP1("1");
        model.setP2("2");
        model.setP3(3);
        model.setP4(4);
        model.setP5("5");
        model.setP6("6");
        model.setP7("7");
        model.setP8("8");
        model.setP9("9");
        list.add(model);
        return list;
    }

    private List<ExportInfo> getList() {
        List<ExportInfo> list = new ArrayList<>();
        ExportInfo info = new ExportInfo();
        info.setAddress("lala");
        info.setAge("22");
        info.setEmail("wjwei@@");
        info.setName("wjwei");
        list.add(info);
        return list;
    }
}
